package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.worktablevo.WorkResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.domain.base.Department;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.exception.OrderException;
import com.blueocean_health.care.management.service.WorktableService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author ChenPeng cp
 * @Date 2017/8/24 17:55
 */

@RestController
@RequestMapping(value = "/web/department", produces = MediaType.APPLICATION_JSON_VALUE)
public class WorktableController{

    @Autowired
    private WorktableService worktableService;

    @RequestMapping("/sort")
    public SysResult getDepartmentList() throws OrderException {
    	User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
        String hid =  courrentUser.getHospitalId();
        SysResult sysResult = worktableService.getListByHospitalIdForSort(hid);
        return sysResult;
    }

    @RequestMapping("/sort/update")
    public SysResult updateDepartmentSort(@RequestBody List<Department> list) throws OrderException {
        SysResult sysResult = worktableService.updateSort(list);
        return sysResult;
    }

    @RequestMapping("/workTable")
    public SysResult getWorkTableByHospital(@RequestBody String jsonString) throws OrderException {
        JSONObject jsonObject =  JSONObject.parseObject(jsonString);
        String clickDate = (String) jsonObject.get("clickDate");
    	User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
        String hid =  courrentUser.getHospitalId();
        SysResult sysResult = worktableService.getWorkTableByHospital(hid,clickDate);
        return sysResult;
    }


    /**
     * 导出excel
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/workTable/excel")
    public void getWorkTableExcel(HttpServletRequest request, HttpServletResponse response,String clickDate) throws Exception{
        try{
            /*JSONObject jsonObject =  JSONObject.parseObject(jsonString);
            String clickDate = (String) jsonObject.get("clickDate");*/
            User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
            String hospitalId=courrentUser.getHospitalId();

            List<WorkResultVo> results = worktableService.getWorkTableByHospitalForExcel(hospitalId,clickDate);

            String finalFileName = URLEncoder.encode("上工展板表"+clickDate+".xls", "UTF8");
            response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());

            ExcelUtil.getInstance().exportObj2ExcelForWorkTable(toClient, results, WorkResultVo.class);
        }catch(IOException e){
            e.printStackTrace();
        }

    }



}
